using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;


namespace Common
{
    /// <summary>
    /// 
    /// </summary>
    public class ImageUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <param name="imgFormat"></param>
        /// <returns></returns>
        public static StreamReader GetImageStreamReader(Image img, ImageFormat imgFormat)
        {
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            img.Save(memStream, imgFormat);
            return new StreamReader(memStream);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="icon"></param>
        /// <returns></returns>
        public static StreamReader GetIconStreamReader(Icon icon)
        {
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            icon.Save(memStream);
            return new StreamReader(memStream);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <param name="imgFormat"></param>
        /// <returns></returns>
        public static byte[] GetImageBytes(System.Drawing.Image img, ImageFormat imgFormat)
        {
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            img.Save(memStream, imgFormat);
            byte[] bytes = memStream.ToArray();
            return bytes;
        }


        /// <summary></summary>
        public static Image ToImage(byte[] contents)
        {
            System.IO.MemoryStream memStream = new System.IO.MemoryStream(contents);
            System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);
            return img;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        public static System.Drawing.Image ScaleToFit(System.Drawing.Image img, int width)
        {
            float scaleFactor = (float) width/img.Width;
            Size newImgSize = new Size((int) scaleFactor*img.Width, (int) scaleFactor*img.Height);
            Bitmap newImg = new Bitmap(newImgSize.Width, newImgSize.Height);
            Graphics g = Graphics.FromImage(newImg);
            g.DrawImage(img, new RectangleF(0, 0, newImg.Width, newImg.Height),
                        new RectangleF(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
            g.Dispose();
            return newImg;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serializedImg"></param>
        /// <param name="imgFormat"></param>
        /// <returns></returns>
        public static System.Drawing.Image ToImage(string serializedImg, System.Drawing.Imaging.ImageFormat imgFormat)
        {
            byte[] bytes = Base64Util.DecodeAsBytes(serializedImg);
            System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes);
            System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);
            return img;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serializedImg"></param>
        /// <returns></returns>
        public static System.Drawing.Image ToImage(string serializedImg)
        {
            byte[] bytes = Base64Util.DecodeAsBytes(serializedImg);
            System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes);
            System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);
            return img;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serializedIcon"></param>
        /// <returns></returns>
        public static System.Drawing.Icon ToIcon(string serializedIcon)
        {
            byte[] bytes = Base64Util.DecodeAsBytes(serializedIcon);
            System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes);
            //System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);
            //return (System.Drawing.Icon)img;
            System.Drawing.Icon icon = new System.Drawing.Icon(memStream);
            return icon;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="contents"></param>
        /// <returns></returns>
        public static Icon ToIcon(byte[] contents)
        {
            System.IO.MemoryStream memStream = new System.IO.MemoryStream(contents);
            System.Drawing.Icon icon = new System.Drawing.Icon(memStream);
            return icon;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <param name="imgFormat"></param>
        /// <returns></returns>
        public static string SerializeImage(System.Drawing.Image img, System.Drawing.Imaging.ImageFormat imgFormat)
        {
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            img.Save(memStream, imgFormat);
            byte[] bytes = memStream.ToArray();
            return Base64Util.EncodeAsBase64(bytes);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public static string SerializeImage(System.Drawing.Image img)
        {
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            img.Save(memStream, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] bytes = memStream.ToArray();
            return Base64Util.EncodeAsBase64(bytes);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="icon"></param>
        /// <returns></returns>
        public static string SerializeIcon(System.Drawing.Icon icon)
        {
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            icon.Save(memStream);
            byte[] bytes = memStream.ToArray();
            return Base64Util.EncodeAsBase64(bytes);
        }
    }
}
